#include "binaryHeap.h"
#include <fstream>
#include <iostream>
#include <sys/time.h>
#include <cmath>

#define SIZE 262144
//#define SIZE 100
int main()
{
	ofstream file1, file2;
	struct timeval start, end;
	struct timezone aux;
	double t;
	
	Heap heap;
	int i, j=0, k=3;
	//Incert Experimental complexity
	file1.open("results/BHincertTime.txt");
	file1 << "# n time(usec)" << endl;
	file2.open("results/BHincertUp.txt");
        file2 << "# n Iterations" << endl;
	for(i = SIZE; i > 0; i--){
		j++;
		gettimeofday(&start, &aux);
		heap.incert(j, i);
		gettimeofday(&end, &aux);
		t  = end.tv_usec - start.tv_usec;
		if(j == k){
			k=k+1234;
			//k=k*2;	
			file1 << j << " " << t << endl;
			file2 << j << " " << heap.numHeapUp << endl;
		}
	}
	file1.close();file2.close();
	cout << "Incert Finish" << endl;
	//Update experimental complexity
	//chose always the larger elements, last element in the tree	
	file1.open("results/BHupdateTime.txt");
	file1 << "# n time(usec)" << endl;
        file2.open("results/BHupdateFindAndUp.txt");
        file2 << "# n Iterations(find and heapfyup)" << endl;
	j=0;
	k=1;
	for(i = SIZE; i > 0; i--){
		j++;
		gettimeofday(&start, &aux);
                heap.updateKey(Vertex(j,i), j*-1);
		gettimeofday(&end, &aux);
                t  = (end.tv_sec*1000000 + end.tv_usec) - (start.tv_sec*1000000+start.tv_usec);
                if(j == k){
			k=k+1234;
                	file1 << j << " " << t << endl;
                	file2 << j << " " << heap.numHeapUp+heap.numHeapDown << endl;
		}
	}
	//heap.printHeap(0);
	file1.close();file2.close();
	cout << "Update Finish" << endl;

        file1.open("results/BHdeleteTime.txt");
        file1 << "# n time(usec)" << endl;
        file2.open("results/BHdeleteDown.txt");
        file2 << "# n Iterations(heapfyDown)" << endl;
        k=SIZE; //log2(SIZE);
	for(i = SIZE; i > 0; i--){
                gettimeofday(&start, &aux);
                heap.removeElement(0);
                gettimeofday(&end, &aux);
                t  = end.tv_usec - start.tv_usec;
                //if(i == pow(2.0,k)){
                if(i == k){
			k = k-1234;		        //k--;
                        file1 << i << " " << t << endl;
                        file2 << i << " " << (heap.numHeapDown) << endl;
                }
        }
	file1.close();file2.close();
	cout << "Delete Finish" << endl;
	heap.printHeap(0);
	return 0;
}
